package cn.shop.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.front.service.BackSettingService;
import cn.shop.front.service.FrontUserService;
import cn.shop.front.util.CurrentUserUtil;
import cn.shop.transcation.backSetting.entity.BackSetting;
import cn.shop.transcation.frontUserModule.FrontUser;
import cn.shop.utils.ApiCode;
import cn.shop.utils.ApiResult;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.SystemSettingConstant.*;

/**
 * 账号预览接口
 *
 * @author : 李一涵
 * @date : Created in 2022-2-21 14:16
 * @description :
 */
@SaCheckLogin
@RestController
@RequestMapping("/api/v1/home")
public class HomeController {

    @Resource
    private FrontUserService frontUserService;
    @Resource
    private BackSettingService backSettingService;

    /**
     * 账号详情
     * @return 返回结果
     */
    @GetMapping("/userinfo")
    public ApiResult<Map<String, Object>> userInfo() {
        FrontUser user = frontUserService.getById(CurrentUserUtil.getUserId());
        if (user == null) {
            return ApiResult.fail(ApiCode.NOT_USER);
        }
        return ApiResult.success(Putif.start()
                .anyway("realName", DesensitizedUtil.chineseName(user.getName()))
                .anyway("idCard", DesensitizedUtil.idCardNum(user.getIdentityCard(), 2,2))
                .anyway("mobile", DesensitizedUtil.mobilePhone(user.getPhone()))
                .get());
    }

    /**
     * 账号安全
     * @return 返回结果
     */
    @GetMapping("/security")
    public ApiResult<Map<String, Object>> accountSecurity() {
        FrontUser user = frontUserService.getById(CurrentUserUtil.getUserId());
        if (user == null) {
            return ApiResult.fail(ApiCode.NOT_USER);
        }
        return ApiResult.success(Putif.start()
                .anyway("certification", StrUtil.isNotBlank(user.getName()) && StrUtil.isNotBlank(user.getIdentityCard()))
                .anyway("hasTransactionPassword", StrUtil.isNotBlank(user.getTransactionPassword()))
                .anyway("transactionPassword", DesensitizedUtil.password(user.getTransactionPassword()))
                .get());
    }

    /**
     * 我的资产
     * @return 返回结果
     */
    @GetMapping("/amount")
    public ApiResult<Map<String, Object>> myAssets() {
        FrontUser user = frontUserService.getById(CurrentUserUtil.getUserId());
        if (user == null) {
            return ApiResult.fail(ApiCode.NOT_USER);
        }
        return ApiResult.success(Putif.start()
                        .anyway("balance", user.getBalance())
                        .anyway("frozen", user.getFrozenAmount())
                .get());
    }

    /**
     * 查看提现费率信息
     *
     * @return 返回结果
     */
    @GetMapping("/rate")
    public ApiResult<Map<String, Object>> rateDetail() {
        List<String> keys = Arrays.asList(P_BANK_WITHDRAW_SHARE, P_ALI_WITHDRAW_SHARE,
                P_ALI_WITHDRAW_SUB, P_BANK_WITHDRAW_SUB, WITHDRAW_DAY_COUNT, AUTO_WITHDRAW_MIN, AUTO_WITHDRAW_MAX);
        List<BackSetting> list = backSettingService.list(Wrappers.<BackSetting>lambdaQuery().in(BackSetting::getKey, keys));
        Map<String, Object> map = new HashMap<>();
        list.forEach(setting -> map.put(setting.getKey(), setting.getValue()));
        return ApiResult.success(map);
    }

}
